package cn.powerone.scm.service.impl;

import java.math.BigDecimal;
import java.util.List;
import cn.powerone.common.utils.DateUtils;
import cn.powerone.scm.domain.ScmCustomerOrders;
import cn.powerone.scm.domain.ScmOrderReceivables;
import cn.powerone.scm.domain.ScmSupplierOrders;
import cn.powerone.scm.service.IScmSupplierOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.powerone.scm.mapper.ScmOrderPaymentMapper;
import cn.powerone.scm.domain.ScmOrderPayment;
import cn.powerone.scm.service.IScmOrderPaymentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 付款Service业务层处理
 * 
 * @author PowerOne
 * @date 2025-08-05
 */
@Service
public class ScmOrderPaymentServiceImpl implements IScmOrderPaymentService 
{
    @Autowired
    private ScmOrderPaymentMapper scmOrderPaymentMapper;
    @Autowired
    private IScmSupplierOrdersService scmSupplierOrdersService;

    /**
     * 查询付款
     * 
     * @param id 付款主键
     * @return 付款
     */
    @Override
    public ScmOrderPayment selectScmOrderPaymentById(Long id)
    {
        return scmOrderPaymentMapper.selectScmOrderPaymentById(id);
    }

    /**
     * 查询付款列表
     * 
     * @param scmOrderPayment 付款
     * @return 付款
     */
    @Override
    public List<ScmOrderPayment> selectScmOrderPaymentList(ScmOrderPayment scmOrderPayment)
    {
        return scmOrderPaymentMapper.selectScmOrderPaymentList(scmOrderPayment);
    }

    /**
     * 新增付款
     * 
     * @param scmOrderPayment 付款
     * @return 结果
     */
    @Override
    @Transactional
    public int insertScmOrderPayment(ScmOrderPayment scmOrderPayment)
    {
        ScmSupplierOrders scmSupplierOrders =scmSupplierOrdersService.selectScmSupplierOrdersById(scmOrderPayment.getOrderId());

        if (scmSupplierOrders == null){
            throw new RuntimeException("订单编号非法");
        }
        BigDecimal newTotalAmount = scmSupplierOrders.getPayedAmount().add(scmOrderPayment.getMoney());
        scmSupplierOrders.setPayedAmount(newTotalAmount);
        scmSupplierOrdersService.updateScmSupplierOrders(scmSupplierOrders);

        scmOrderPayment.setSupplierId(scmSupplierOrders.getSupplierId());
        scmOrderPayment.setCreateTime(DateUtils.getNowDate());
        return scmOrderPaymentMapper.insertScmOrderPayment(scmOrderPayment);
    }

    /**
     * 修改付款
     * 
     * @param scmOrderPayment 付款
     * @return 结果
     */
    @Override
    public int updateScmOrderPayment(ScmOrderPayment scmOrderPayment)
    {
        ScmOrderPayment scmOrderPaymentOld = scmOrderPaymentMapper.selectScmOrderPaymentById(scmOrderPayment.getId());
        if (scmOrderPaymentOld == null){
            throw new RuntimeException("付款编号非法");
        }
        ScmSupplierOrders scmSupplierOrders =scmSupplierOrdersService.selectScmSupplierOrdersById(scmOrderPayment.getOrderId());
        if (scmSupplierOrders == null){
            throw new RuntimeException("订单编号非法");
        }
        BigDecimal newTotalAmount = scmSupplierOrders.getPayedAmount().subtract(scmOrderPaymentOld.getMoney()).add(scmOrderPayment.getMoney());
        scmSupplierOrders.setPayedAmount(newTotalAmount);
        scmSupplierOrdersService.updateScmSupplierOrders(scmSupplierOrders);

        scmOrderPayment.setUpdateTime(DateUtils.getNowDate());
        return scmOrderPaymentMapper.updateScmOrderPayment(scmOrderPayment);
    }

    /**
     * 批量删除付款
     * 
     * @param ids 需要删除的付款主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteScmOrderPaymentByIds(Long[] ids)
    {
        if (ids == null || ids.length == 0){
            throw new RuntimeException("请选择要删除的回款");
        }
        ScmOrderPayment scmOrderPayment = scmOrderPaymentMapper.selectScmOrderPaymentById(ids[0]);
        ScmSupplierOrders scmSupplierOrders =scmSupplierOrdersService.selectScmSupplierOrdersById(scmOrderPayment.getOrderId());
        if (scmSupplierOrders == null){
            throw new RuntimeException("订单编号非法");
        }
        BigDecimal newTotalAmount = scmSupplierOrders.getPayedAmount().subtract(scmOrderPayment.getMoney());
        scmSupplierOrders.setPayedAmount(newTotalAmount);
        scmSupplierOrdersService.updateScmSupplierOrders(scmSupplierOrders);

        return scmOrderPaymentMapper.deleteScmOrderPaymentByIds(ids);
    }

    /**
     * 删除付款信息
     * 
     * @param id 付款主键
     * @return 结果
     */
    @Override
    public int deleteScmOrderPaymentById(Long id)
    {
        return scmOrderPaymentMapper.deleteScmOrderPaymentById(id);
    }
}
